package com.wyyc.common.register.service.impl;

import com.alibaba.fastjson.JSON;
import com.sun.deploy.net.URLEncoder;
import com.wyyc.common.register.service.RegisterService;
import com.wyyc.common.user.repository.UserRepository;
import com.wyyc.common.user.service.UserService;
import com.wyyc.entity.MessageCode;
import com.wyyc.entity.ResJson;
import com.wyyc.entity.UsersEntity;
import com.wyyc.ext.ExtInterface;
import org.springframework.stereotype.Service;
import org.tuckey.web.filters.urlrewrite.utils.URLDecoder;

import javax.annotation.Resource;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hwei~ on 2017/6/6.
 */
@Service
public class RegisterServiceImpl implements RegisterService{

    private static List<MessageCode> messageCodeList = new ArrayList<MessageCode>();

    @Resource
    private UserService userService;
    @Resource
    private UserRepository userRepository;

    @Override
    public ResJson sendCode(String phone) {
        ResJson resJson = new ResJson();
        //判断手机是否被注册
        if (userService.accoPhoneToUser(phone)==null) {
            //未注册
            //获取并发送短信验证码
            //String code = ExtInterface.getCode(phone);
            String code = "123456";
            //判断短信是否发送成功
            if (code!=null) {
                //成功
                //循环检查临时保存有无此手机
                int x = 0;
                for (MessageCode m:messageCodeList){
                    //有的话，覆盖验证码
                    if (m.getPhone().equals(phone)){
                        m.setCode(code);
                        x++;
                    }
                }
                //没有,就添加
                if (x==0){
                    MessageCode messageCode = new MessageCode();
                    messageCode.setPhone(phone);
                    messageCode.setCode(code);
                    messageCodeList.add(messageCode);
                }
                //返回状态
                resJson.setStatus(0);
                resJson.setDesc("短信验证码发送成功!");
                return resJson;
            }
            //失败
            //返回状态
            resJson.setStatus(1);
            resJson.setDesc("短信验证码发送失败!");
            return resJson;
        }
        //已注册
        //返回状态
        resJson.setStatus(2);
        resJson.setDesc("当前手机已被注册!");
        return resJson;
    }

    @Override
    public ResJson addUser(String phone, String code) {
        ResJson resJson = new ResJson();
        //判断手机是否被注册
        if (userService.accoPhoneToUser(phone)==null) {
            //判断验证码是否正确
            int x = 0;
            for (MessageCode m:messageCodeList){
                if (m.getPhone().equals(phone)&&m.getCode().equals(code)){
                    x++;
                }
            }
            if (x>0){
                UsersEntity usersEntity = new UsersEntity();
                usersEntity.setPhone(phone);
                usersEntity.setAddate(new Date(System.currentTimeMillis()));
                usersEntity.setRegedditTime(new Timestamp(System.currentTimeMillis()));
                usersEntity.setStatus(1);
                usersEntity.setMoney(0);
                try{
                    userRepository.save(usersEntity);
                }catch (Exception e){

                }
                UsersEntity user = userRepository.findByPhone(phone);
                resJson.setResult(JSON.toJSONString(user));
                resJson.setDesc("用户注册成功!");
                resJson.setStatus(0);
                return resJson;
            }
            resJson.setResult("");
            resJson.setDesc("验证码错误!");
            resJson.setStatus(1);
            return resJson;
        }
        resJson.setResult("");
        resJson.setDesc("该用户已被注册!");
        resJson.setStatus(2);
        return resJson;
    }
}
